package com.buaa.controller;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.buaa.service.InstitutionService;
import com.buaa.utils.JacksonUtil;
import com.buaa.utils.PieChart3D;

/**
 * Title: 机构控制器 <br/>
 * Description: <br/>
 * Company: BUAA
 *
 * @author 伟宏
 * @date 2017年1月15日
 * @modify 2017年04月30日
 */
@Controller
@RequestMapping("institution")
public class InstitutionController {

	@Autowired
	private InstitutionService institutionService;

	/**
	 * 统计信息界面
	 */
	@RequestMapping("dw")
	public String getInstitution() {
		return "/dw";
	}

	/**
	 * 统计信息服务
	 * 
	 * @param zhibiao
	 * @param curlevel
	 * @param curcenterlng
	 * @param curcenterlat
	 * @param loctype
	 * @param session
	 * @return
	 */
	@RequestMapping("getSum")
	@ResponseBody
	public String getSum(String zhibiao, String curlevel, String curcenterlng, String curcenterlat, String loctype, HttpSession session) {
		session.setAttribute("curlevel", curlevel);
		session.setAttribute("curcenterlng", curcenterlng);
		session.setAttribute("curcenterlat", curcenterlat);
		return institutionService.sumInfo(loctype);
	}

	/**
	 * 统计信息服务 - 获取柱状图
	 * 
	 * @param quyu
	 * @param loctype
	 * @param zhibiao
	 * @param session
	 * @return
	 */
	@RequestMapping("getCount")
	@ResponseBody
	public String getCount(String quyu, String loctype, String zhibiao, HttpSession session) {
		System.out.println("teee");
		Map<String, Long> metaData = institutionService.getCount(quyu, loctype, zhibiao);
		String filename = "";
		try {
			filename = PieChart3D.generateRoChart(metaData, 600, 360, session, new PrintWriter("asdf"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		String graphURL = "/Servlet/DisplayChart?filename=" + filename;
		return graphURL;
	}

	/**
	 * 内容搜索界面
	 * 
	 * @return
	 */
	@RequestMapping("dwcx")
	public ModelAndView getDwcx() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("dwcx");
		mav.addObject("institutionTypeList", institutionService.findAllType());
		mav.addObject("kindList", institutionService.findAllKind());
		mav.addObject("techList", institutionService.findAllTech());
		return mav;
	}

	/**
	 * 内容搜索服务
	 * 
	 * @param loctype
	 * @param dwsx
	 * @param dwmc
	 * @param dwmccxfs
	 * @param dwjm
	 * @param dwjsly
	 * @return
	 */
	@RequestMapping("getInstitution")
	@ResponseBody
	public String getInstitution(String loctype, String dwlx, String dwmc, String dwmccxfs, String dwjm, String dwjsly) {
		// return institutionService.contentSearch(loctype, dwmc, dwlx,
		// dwmccxfs, dwjm, dwjsly);
		return institutionService.contentSearch2(loctype, dwmc, dwlx, dwmccxfs, dwjm, dwjsly);
	}

	/**
	 * 获取alllist信息
	 * 
	 * @param loctype
	 * @return
	 */
	@RequestMapping("dwalllist")
	public ModelAndView getAllList(String loctype) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("hljlayer/dwalllist");
		mav.addObject("loctype", loctype);
		mav.addObject("zhibiao", "city");
		List<Object[]> ret = institutionService.getAllList(loctype);
		if (ret != null) {
			mav.addObject("AllList", ret);
		}
		return mav;
	}

	/**
	 * 获取countlist信息
	 * 
	 * @param quyu
	 * @param loctype
	 * @param zhibiao
	 * @return
	 */
	@RequestMapping("dwcountlist")
	public ModelAndView getCountList(String quyu, String loctype, String zhibiao) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("hljlayer/dwcountlist");
		mav.addObject("zhibiao", zhibiao);
		mav.addObject("quyu", quyu);
		mav.addObject("loctype", loctype);
		List<Object> ret = institutionService.getCountList(quyu, loctype, zhibiao);
		if (ret != null) {
			mav.addObject("name", ret.get(0));
			mav.addObject("CountList", ret.get(1));
		}
		return mav;
	}

	/**
	 * 获取detail信息
	 * 
	 * @param quyu
	 * @return
	 */
	@RequestMapping("dwdetail")
	public ModelAndView getDetail(String quyu) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("hljlayer/dwdetail");
		List<Object[]> ret = institutionService.getDetail(quyu);
		if (ret != null) {
			Object[] xm = ret.get(0);
			mav.addObject("name", xm[0]);
			mav.addObject("city", xm[1]);
			mav.addObject("county", xm[9]);
			mav.addObject("type_name", xm[2]);
			mav.addObject("depart", xm[3]);
			mav.addObject("addr", xm[4]);
			mav.addObject("birth", xm[5]);
			mav.addObject("contact_name", xm[6]);
			mav.addObject("contact_job", xm[7]);
			mav.addObject("contact_phone", xm[8]);
		}
		return mav;
	}

	/**
	 * 获取cx表1数据
	 * 
	 * @param yqmc
	 * @param ssdw
	 * @param yqmccxfs
	 * @param quyu
	 * @param loctype
	 * @return
	 */
	@RequestMapping("dwcxtable1")
	public ModelAndView getDwcxTable1(String dwlx, String dwmc, String dwmccxfs, String dwjm, String dwjsly, String quyu, String loctype) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("hljlayer/dwcxtable1");
		// mav.addObject("tableInfoList",institutionService.getCxTable1Info(dwlx,
		// dwmc, dwmccxfs, dwjm, dwjsly, quyu, loctype));
		mav.addObject("tableInfoList", institutionService.getCxTable1Info2(dwlx, dwmc, dwmccxfs, dwjm, dwjsly, quyu, loctype));
		return mav;
	}

	/**
	 * 获取表1数据
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("dwtable1")
	public ModelAndView getTable1Info(String zhibiao, String zhibiaomc, String quyu, String loctype, String page) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("hljlayer/dwtable1");
		mav.addObject("zhibiao", zhibiao);
		mav.addObject("zhibiaomc", zhibiaomc);
		mav.addObject("quyu", quyu);
		mav.addObject("loctype", loctype);
		List<Object> ret = institutionService.getTable1Info(zhibiao, zhibiaomc, quyu, loctype, page);
		if (ret != null) {
			mav.addObject("totalpage", ret.get(0));
			mav.addObject("total", ret.get(1));
			mav.addObject("curpage", ret.get(2));
			mav.addObject("nextpage", ret.get(3));
			mav.addObject("lastpage", ret.get(4));
			mav.addObject("pagenum", ret.get(5));
			mav.addObject("beginnum", ret.get(6));
			mav.addObject("endnum", ret.get(7));
			mav.addObject("tableInfoList", ret.get(8));
		}
		return mav;
	}

	/**
	 * 获取表2数据
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("dwtable2")
	public ModelAndView getTable2Info(String id) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("hljlayer/dwtable2");
		List<Object[]> ret = institutionService.getTable2Info(id);
		if (ret != null) {
			Object[] xm = ret.get(0);
			mav.addObject("name", xm[0]);
			mav.addObject("city", xm[1]);
			mav.addObject("county", xm[9]);
			mav.addObject("type_name", xm[2]);
			mav.addObject("depart", xm[3]);
			mav.addObject("addr", xm[4]);
			mav.addObject("birth", xm[5]);
			mav.addObject("contact_name", xm[6]);
			mav.addObject("contact_job", xm[7]);
			mav.addObject("contact_phone", xm[8]);
		}
		return mav;
	}

	/**
	 * 获取比较的数据
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("dwcp")
	public ModelAndView getCp(String totalid, HttpSession session) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("hljlayer/dwcp");
		String[][] data = institutionService.getCp(totalid);
		mav.addObject("data", data);
		String filename = "";
		try {
			filename = PieChart3D.generateRoChartdwcp(data, 600, 375, session, new PrintWriter("pic"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		String graphURL = "/Servlet/DisplayChart?filename=" + filename;
		mav.addObject("graphURL", graphURL);
		return mav;
	}
	
	

	/**
	 * 资源指数分析
	 * 
	 * @return
	 */
	@RequestMapping("indexAnalysis")
	public ModelAndView getIndex() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("hljlayer/indexAnalysis");
		mav.addObject("resource", "institution");
		return mav;
	}

	@RequestMapping("sum")
	@ResponseBody
	public String getSum(String zhibiao) {
		return institutionService.getSum(zhibiao);
	}

	@RequestMapping("fb")
	@ResponseBody
	public String getFb() {
		return JacksonUtil.toJSon(institutionService.getFb());
	}

	@RequestMapping("fb2")
	@ResponseBody
	public String getFb2() {
		return JacksonUtil.toJSon(institutionService.getFb2());
	}

	@RequestMapping("citySum")
	@ResponseBody
	public String getCitySum() {
		return institutionService.getCitySum();
	}

}
